Skip to content

Proposal: QGEMeans, mini-batch e-means with SymphonyQG assignment - #261

Draft
TamimEhsan wants to merge 7 commits into
VectorDB-NTU:mainfrom
TamimEhsan:qg-e-means
Draft

TamimEhsan wants to merge 7 commits into
VectorDB-NTU:mainfrom
TamimEhsan:qg-e-means

Conversation

@TamimEhsan

Copy link
Copy Markdown
Member

QGEMeans, mini-batch e-means with SymphonyQG assignment

Adds rabitqlib::qgemeans::QGEMeans (C++) and rabitqlib.QGEMeans (Python). It is the e-means recipe (arXiv:2611.09501, Algorithm 1, with the constants from Faiss contrib/e_means), with each mini-batch assigned through a SymphonyQG graph over the current centroids. This is a new class; QGKMeans and RaBitQKMeans keep their current behaviour.

  • Training: a seeded shuffle each epoch, then fixed-size mini-batches; the batch size follows the batch-size law. Before every mini-batch the raw-vector centroid graph is rebuilt and searched top-1. Centroids take an EMA mass-weighted step under the step law with a cosine schedule. Every five mini-batches, centroids with little mass are revived by splitting heavy ones. The last epoch's means become the final centroids.
  • API: the same as the other clustering classes: train, train_centroids, assign, centroids, assignments, distances, iteration_stats (one entry per epoch) and final_obj. final_assignment is 'approximate' by default, or 'exact'. A failed train keeps the previous results, and results are identical for any thread count.
  • Parameters: epochs=8, batch_size=0 (use the batch-size law), max_degree=32, ef_construction=240, ef_search=16, graph_build_iterations=1, seed, num_threads, verbose. Python uses the index-style names from the start, so there are no deprecated aliases.
  • Shared code: parameter checks, the trained-model assign front end and the final-assignment dispatch now live in clustering::detail, and the centroid-graph checks in qgkmeans::detail. All three classes use them, and the existing error messages are unchanged. QGAssigner::search_points takes an optional row index list, so mini-batches are searched in place without copying them.
  • Also included: a sample (bin/qgemeans), C++ and Python tests, the type stub, and docs in clustering.md.

Benchmark

GIST1M, k = 4096, seed 42, Xeon Gold 6418H, GCC 14 -march=native, 8 threads pinned to physical cores 4–11. Two interleaved runs of each.

Method Train time Exact WCSS Recall@10 Imbalance
QGKMeans, 25 iterations 33.8 / 34.0 s 1.03471e6 0.8159 1.413
QGEMeans, 8 epochs 20.7 / 20.7 s (1.64× faster) 1.03166e6 (−0.3%) 0.8185 1.257
  • QGEMeans reproduces the standalone prototype bit for bit; its time was 21.0 s.
  • About a third of QGEMeans training (7.0 of 21.0 s) is rebuilding the centroid graph.

Next: quantized graph build

I'm working on a quantized (8-bit, AVX-512 VNNI) SymphonyQG graph build. On a full GIST1M index it cuts build time from 31.8 s to 19.4 s at 48 threads, and from 662 s to 407 s on one thread, with recall within noise. QGEMeans rebuilds its centroid graph before every mini-batch, so a faster build should lower its total time further. That will come as a follow-up PR once this one is merged.

Checks

  • clang-format and full clang-tidy.
  • The C++ suite: 518 tests.
  • The installed CMake consumer test.
  • The Python suite: 1227 tests, plus the mypy editor checks.
  • mkdocs build --strict.
  • Not run: Pyright, which needs Node 24; CI runs it.

🤖 Generated with Claude Code

@gouyt13

gouyt13 commented Nov 21, 2026

Copy link
Copy Markdown
Member

Thanks for the proposal! 0.9 is in its final release steps, so we're holding new features until it lands on main. Once it does, could you rebase this branch onto the new main? It starts from main before the 0.9 work, and a trial merge with dev/0.9 conflicts in seven files. The main one is qgkmeans.hpp: on dev/0.9, QGAssigner rejects METRIC_COSINE (#256), the QGKMeans parameters are renamed (#233), and the k-means coordinate bound changed (#249). So the moved qg_assigner.hpp needs to start from the 0.9 version.

After the rebase we'll review it in full. In case you want to prepare, we'll check:

  • that QGKMeans and RaBitQKMeans give the same results as before the shared-helper refactor;
  • the benchmark on a second dataset, with the exact commands;
  • the e-means references (the paper and the Faiss contrib code) for the constants you use.

@gouyt13

gouyt13 commented Nov 23, 2026

Copy link
Copy Markdown
Member

0.9.0 is now on main (v0.9.0). Could you rebase this branch onto the current main when you have time? The notes in my previous comment still apply, especially starting the moved qg_assigner.hpp from the 0.9 version. Then we'll do the full review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants